/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 029. 排序的循环链表
 * @author: WXY
 * @create: 2022-12-17 21:14
 * @Version 1.0
 **/

public class Num_offer2_029_insert {
    static class Node {
        public int val;
        public Node next;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _next) {
            val = _val;
            next = _next;
        }
    }

    ;


    public static Node insert(Node head, int insertVal) {
        Node insertNode = new Node(insertVal);
        if (head == null) {
            Node node = insertNode;
            node.next = node;
            return node;
        }
        if (head.next == head) {
            Node node = insertNode;
            head.next = node;
            node.next = head;
            return head;
        }
        Node findHead = head.next;
        int min = head.val;
        int max = head.val;
        Node minNode = head;
        Node maxNode = head;
        while (findHead != head ) {
            if (findHead.val < min) {
                minNode = findHead;
                min = minNode.val;
            }
            if (findHead.val > max) {
                maxNode = findHead;
                max = findHead.val;
            }
            findHead = findHead.next;
        }
        if (maxNode.val == maxNode.next.val) {
            maxNode = maxNode.next;
        }
        //此时最打的节点是maxNode


        if (insertVal <= minNode.val || insertVal >= maxNode.val) {
            insertMaxMin(maxNode, insertNode);
            return head;
        }

        Node curNode = minNode;
        while (curNode.next != minNode) {
            if (curNode.val < insertVal && curNode.next.val < insertVal) {
                curNode = curNode.next;
            } else {
                insertNode.next = curNode.next;
                curNode.next = insertNode;
                break;
            }
        }

        return head;
    }

    private static void insertMaxMin(Node maxNode, Node insertNode) {
        insertNode.next = maxNode.next;
        maxNode.next = insertNode;
    }

    public static void printNode(Node head) {
        Node cur = head.next;
        System.out.print(head.val + " ");
        while (cur != head) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head1 = new Node(10);
        Node head2 = new Node(13);
        Node head3 = new Node(18);
        Node head4 = new Node(19);
        Node head5 = new Node(20);
        Node head6 = new Node(21);
        Node head7 = new Node(22);
        Node head8 = new Node(23);
        Node head9 = new Node(24);
        Node head10 = new Node(25);
        head1.next = head2;
        head2.next = head3;
        head3.next = head4;
        head4.next = head5;
        head5.next = head6;
        head6.next = head7;
        head7.next = head8;
        head8.next = head9;
        head9.next = head10;
        head10.next = head1;
        printNode(head1);
        Node insert = insert(head1, 1);
        printNode(head1);


    }
}
